package com.glory.dmegcpda.adapter

import android.widget.RadioButton
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.glory.dmegcpda.R
import com.glory.dmegcpda.bean.LevelBean

/**
 * FileName: LevelAdapter
 * Author: XJH
 * Date: 2021/10/13 17:31
 * Description:
 * History:
 */
class LevelAdapter (data: MutableList<LevelBean.AdrefBean>? ) :
    BaseQuickAdapter<LevelBean.AdrefBean, BaseViewHolder>(
        R.layout.item_level,
        data as MutableList<LevelBean.AdrefBean>
    ) {
    var position : Int = -1
    var isSelector : Boolean = false
    var level : String = ""
    var mLevel : String = ""
    override fun convert(helper: BaseViewHolder, item: LevelBean.AdrefBean) {
        helper.setText(R.id.rbLevel, item.DESCRIPTION)
        var rbLevel : RadioButton = helper.getView(R.id.rbLevel)
        rbLevel.isChecked = false
        if (level == item.KEYID && !isSelector){
            rbLevel.isChecked = true
            position = helper.adapterPosition
            mLevel = item.KEYID
        }

        rbLevel.setOnClickListener{
            isSelector = true
            when (position) {
                -1 -> {
                    rbLevel.isChecked = true
                    position = helper.adapterPosition
                    mLevel = item.KEYID
                }
                helper.adapterPosition -> {
                    rbLevel.isChecked = false
                    position = -1
                    mLevel = ""
                }
                else -> {
                    notifyItemChanged(position)
                    rbLevel.isChecked = true
                    position = helper.adapterPosition
                    mLevel = item.KEYID
                }
            }
        }
    }
}